package cz.data.domain.masterdata.utils;

import cz.data.common.core.DataConstant;
import cz.data.common.support.db.core.DbColumn;
import cz.data.common.support.db.core.DbTable;
import cz.data.domain.masterdata.model.entity.ModelColumnEntity;
import cz.data.domain.masterdata.model.entity.ModelEntity;

import java.util.List;
import java.util.stream.Collectors;

public class ModelUtils {

    public static DbTable dbTable(ModelEntity model) {
        DbTable table = new DbTable();
        table.setTableName(model.getModelPhysicalTable());
        table.setTableComment(model.getModelName());
        List<DbColumn> columns = model.getModelColumns().stream()
                .map(ModelUtils::dbColumn)
                .collect(Collectors.toList());
        table.setColumns(columns);
        return table;
    }

    public static DbColumn dbColumn(ModelColumnEntity model) {
        DbColumn column = new DbColumn();
        column.setColName(model.getColumnName());
        column.setDataType(model.getColumnType());
        column.setDataLength(model.getColumnLength());
        column.setDataPrecision(model.getColumnScale());
        column.setDataScale(model.getColumnScale());
        column.setColKey(DataConstant.TrueOrFalse.TRUE.equalsKey(model.getIsPk()));
        column.setNullable(DataConstant.TrueOrFalse.FALSE.equalsKey(model.getIsRequired()));
        column.setColPosition(model.getSort());
        column.setDataDefault(model.getDefaultValue());
        column.setColComment(model.getColumnComment());
        return column;
    }

}
